package com.he.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.he.constant.CommonConstant;
import com.he.constant.RedisConstant;
import com.he.domain.vo.SiteConfigVO;
import com.he.entity.SiteConfig;
import com.he.enums.FilePathEnum;
import com.he.mapper.SiteConfigMapper;
import com.he.service.*;
import com.he.strategy.context.UploadStrategyContext;
import com.he.utils.BeanCopyUtils;
import com.he.utils.MinioUtils;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Objects;

/**
 * @author He
 * @version 1.0
 * @Date 2023/9/14 20:33
 * @Desc 网站配置服务实现类
 */
@Service
public class SiteConfigServiceImpl extends ServiceImpl<SiteConfigMapper, SiteConfig> implements SiteConfigService {

    @Resource
    private RedisService redisService;

    @Resource
    private SiteConfigMapper siteConfigMapper;

    @Resource
    private LinkService linkService;

    @Resource
    private PhotoService photoService;

    @Resource
    private AlbumService albumService;

    @Resource
    private MinioUtils minioUtils;

    /**
     * 上传策略上下文
     */
    @Resource
    private UploadStrategyContext uploadStrategyContext;

    @Override
    public SiteConfigVO getSiteConfig() {
        SiteConfigVO siteConfigVO = redisService.getObject(RedisConstant.SITE_CONFIG_KEY);
        if (Objects.isNull(siteConfigVO)){
            SiteConfig siteConfig = siteConfigMapper.selectById(1);
            siteConfigVO = BeanCopyUtils.copyBean(siteConfig, SiteConfigVO.class);
            siteConfigVO.setQuickLinkList(linkService.getQuickLinkList());
            siteConfigVO.setTechniqueLinkList(linkService.getTechniqueLinkList());
            siteConfigVO.setCarouselList(photoService.getCarouselsPhotoList());
            siteConfigVO.setAlbumVOList(albumService.listAlbumVO());
            redisService.setObject(RedisConstant.SITE_CONFIG_KEY,siteConfigVO);
        }
        return siteConfigVO;
    }

    @Override
    public void updateSiteConfig(SiteConfig siteConfig) {
        siteConfig.setId(CommonConstant.SITE_CONFIG_ID);
        updateById(siteConfig);
        redisService.deleteObject(RedisConstant.SITE_CONFIG_KEY);
    }

    @Override
    public String uploadSiteImg(MultipartFile file) {
//        return uploadStrategyContext.executeUploadStrategy(file, FilePathEnum.SITE_CONFIG.getPath());
        return minioUtils.upload(file);
    }
}
